package com.example.cargetter.utils;


import java.util.ArrayList;

/**
 * 处理接收到的经纬度数据
 */
public class UDPDataHandler {

    static double  PI = 3.1415926535897932384626;
    static double A = 6378245.0;
    static double EE = 0.00669342162296594323;


    //参照北纬2429.53531，东经11810.78036，需要做如下计算：24+（29.53531/60 ≈ 24.49225517,118+（10.78036/60）≈118.17967267
    public static double str_To_Gps84(String in_data1, String in_data2){
        double result;
        int len_data1 = in_data1.length();
        int len_data2 = in_data2.length();
        String str_data2 = in_data2;
        //给in_data2补0，
        if(len_data2 < 5) {
            for(int i = len_data2; i <= 5; i ++)
            {
                str_data2 = "0" + str_data2;
            }
        }


        int temp_data = Integer.parseInt(in_data1);
        int symbol = 1;
        if (temp_data < 0) symbol = -1;
        int degree = (int) (temp_data / 100.0);
        String str_decimal = new String(String.valueOf(in_data1.charAt(len_data1-2))) + in_data1.charAt(len_data1-1) + '.' + str_data2;
        double f_degree = Double.parseDouble(str_decimal)/60.0;
        if (symbol > 0) result = degree + f_degree;
        else result = degree - f_degree;
        return result;
    }

    public static ArrayList<Double> gps84_to_gcj02(double lat, double lon) {
        ArrayList<Double> res = new ArrayList<Double>();
        if( outOfChina(lat,lon) ) {
            res.add(0.0);
            res.add(0.0);
            return res;
        }
        double dLat =  transformLat(lon -105.0,lat -35.0);
        double dLon =  transformLon(lon -105.0,lat -35.0);
        double radLat =lat /180.0*PI;
        double magic = Math.sin(radLat);
        magic =1-  EE *magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat =(dLat *180.0)/((  A *(1-  EE))/(magic *sqrtMagic)*  PI);
        dLon =(dLon *180.0)/(  A /sqrtMagic *Math.cos(radLat)*  PI);
        double mgLat =lat + dLat;
        double mgLon = lon + dLon;
        res.add(mgLat);
        res.add(mgLon);
        return res;
    }


    // public void gcj02_to_bd09(self, gg_lat, gg_lon):
    //     x = gg_lon
    //     y = gg_lat
    //     z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y *   PI)
    //     theta = Math.atan2(y, x) + 0.000003 * Math.cos(x *   PI)
    //     bd_lon = z * Math.cos(theta) + 0.0065
    //     bd_lat = z * Math.sin(theta) + 0.006
    //     return [bd_lat, bd_lon]


    public static boolean outOfChina(double lat,double lon) {
        if(lon< 72.004 || lon >137.8347) return true;
        if(lat< 0.8293 || lat >55.8271) return true;
        return false;
    }

    public static ArrayList<Double> transform(double lat,double lon) {
        ArrayList<Double> res = new ArrayList<Double>();
        if(  outOfChina(lat,lon) ) {
            res.add(0.0);
            res.add(0.0);
            return res;
        }
        double dLat =  transformLat(lon -105.0,lat -35.0);
        double dLon =  transformLon(lon -105.0,lat -35.0);
        double radLat =lat /180.0*  PI;
        double magic = Math.sin(radLat);
        magic =1-  EE *magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat =(dLat *180.0)/((  A *(1-  EE))/(magic *sqrtMagic)*  PI);
        dLon =(dLon *180.0)/(  A /sqrtMagic *Math.cos(radLat)*  PI);
        double mgLat =lat + dLat;
        double mgLon = lon + dLon;
        res.add(mgLat);
        res.add(mgLon);
        return res;
    }

    public static double transformLat(double x,double y) {
        double ret =-100.0+2.0*x +3.0*y +0.2*y *y +0.1*x *y +0.2*Math.sqrt(Math.abs(x));
        ret +=(20.0 * Math.sin(6.0*x *  PI)+20.0*Math.sin(2.0*x *  PI))*2.0/3.0;
        ret +=(20.0*Math.sin(y *  PI)+40.0*Math.sin(y /3.0*  PI))*2.0/3.0;
        ret +=(160.0*Math.sin(y /12.0*  PI)+320*Math.sin(y *  PI /30.0))*2.0/3.0;
        return ret;
    }

    public static double transformLon(double x,double y) {
        double ret =300.0+x +2.0*y +0.1*x *x +0.1*x *y +0.1*Math.sqrt(Math.abs(x));
        ret +=(20.0*Math.sin(6.0*x *  PI)+20.0*Math.sin(2.0*x *  PI))*2.0/3.0;
        ret +=(20.0*Math.sin(x *  PI)+40.0*Math.sin(x /3.0*  PI))*2.0/3.0;
        ret +=(150.0*Math.sin(x /12.0*  PI)+300.0*Math.sin(x /30.0*  PI))*2.0/3.0;
        return ret;
    }

    public static ArrayList<Double> hander(String in_data1, String in_data2, String in_data3, String in_data4) {
        double f_lon = str_To_Gps84(in_data1, in_data2);
        double f_lat = str_To_Gps84(in_data3, in_data4);
        System.out.println(f_lat + f_lon);
        ArrayList<Double> gcj02_data = gps84_to_gcj02(f_lat, f_lon);
        System.out.println("gcj02:" + gcj02_data);
        return gcj02_data;
        // bd09_data =  gcj02_to_bd09(gcj02_data[0], gcj02_data[1])
        // System.out.println("bd09:", bd09_data)
    }
    
}
